// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.commands;

import org.dengues.core.process.CompProcessHelper;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.designer.ui.process.models.CompProcess;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-15 下午09:26:13
 */
public class CompNodePasteCommand extends AbstractGEFCommand {

    private final CompNode node;

    private final CompProcess process;

    private final Point newPoint;

    /**
     * Qiang.Zhang.Adolf@gmail.com CompNodePasteCommand constructor comment.
     * 
     * @param node
     * @param point
     * @param nodeCount
     */
    public CompNodePasteCommand(CompProcess process, CompNode node, Point point) {
        setLabel(Messages.getString("CompNodePasteCommand.paste", node.getUniqueName())); //$NON-NLS-1$
        this.node = node;
        this.element = node;
        this.process = process;
        Rectangle rectangle = new Rectangle(point, node.getSize());
        initNewBounds(rectangle);
        newPoint = rectangle.getLocation().getCopy();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.commands.AbstractGEFCommand#execute()
     */
    @Override
    public void execute() {
        CompNode newNode = node.clone();
        newNode.setCompProcess(process);
        newNode.setUniqueName(null);
        newNode.setCompProcess(process);
        Point p = newPoint.getCopy();
        // p.x = node.getLocation().x + node.getSize().width;
        // p.y = node.getLocation().y + node.getSize().height;
        newNode.setLocation(CompProcessHelper.generateElementNewLocation(process, node,
                new Rectangle(p, node.getSize())));
        process.addCompNode(newNode);
        process.addCompNodeLabel(newNode.getNodeLabel());
        process.addCompNodeStatus(newNode.getNodeStatus());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.process.commands.AbstractGEFCommand#undo()
     */
    @Override
    public void undo() {
        process.removeCompNode(node);
        process.removeCompNodeLabel(node.getNodeLabel());
        process.removeCompNodeStatus(node.getNodeStatus());
    }

}
